﻿using System.Collections;
using FairyGUI;

/// <summary>
/// 离开确认
/// </summary>
public class UIGameLeaveConfirm : UIPopupWindow {

	static UIGameLeaveConfirm _inst;
	GButton _leaveBtn;
	GButton _refuseBtn;

	GTextField _title;
	GTextField[] _playerMsg;
	
	public UIGameLeaveConfirm()
	{
		_inst = this;

		// 加载界面
		base.Init(UIPackage.CreateObject("游戏界面", "解散房间确认框").asCom);

		// 拒绝按钮
		_refuseBtn = uiComp.GetChild("知道了").asButton;
		_refuseBtn.onClick.Add(() => {
			confirm(false);
			_Close();
		});

		// 解散房间
		_leaveBtn = uiComp.GetChild("发送请求").asButton;
		_leaveBtn.onClick.Add(() => {
			confirm(true);

			_leaveBtn.visible = _leaveBtn.enabled = 
				_refuseBtn.visible = _refuseBtn.enabled = false;
		});

		_title = uiComp.GetChild ("描述").asTextField;

		_playerMsg = new GTextField[5];
		for (int i = 1; i <= 5; ++i) {
			_playerMsg[i - 1] = uiComp.GetChild ("玩家" + i).asTextField;
			_playerMsg [i - 1].text = "";

			GameDataKernel.GamePlayer gamePlayer = GameDataKernel.inst.GetPlayer (i);
			if (gamePlayer != null) {
				_playerMsg [i - 1].text = gamePlayer.name;
			}
		}
	}

	void confirm(bool ok) {
		Hashtable reqArgs = new Hashtable ();
		reqArgs ["uid"] = DataKernel.inst.userInfo.uid;
		reqArgs ["roomId"] = GameDataKernel.inst.roomId;
		reqArgs["ok"] = ok;

		NetIOController.inst.SendMessage (
			new IOMessage(
				IOCmds.CMSG_PLAYER_RESP_DESTROY_ROOM, 
				IOStates.STATE_OK, 
				new HashtableEx(reqArgs)));
	}

	void SetTitle(int playerIndex) {
		if (playerIndex == GameDataKernel.inst.playerIndex) {
			_refuseBtn.visible = _refuseBtn.enabled =
				_leaveBtn.visible = _leaveBtn.enabled = false;
		}

		_title.text = GameDataKernel.inst.GetPlayer(playerIndex).name + "正在解散房间：";
	}

	void SetMessage(int playerIndex, bool ok) {
		string msg = GameDataKernel.inst.GetPlayer (playerIndex).name + "    ";
		if (ok) {
			msg += "[color=#00ff00]同意[/color]";
		} else {
			msg += "[color=#00ff00]拒绝[/color]";
		}
		_playerMsg [playerIndex - 1].text = msg;
	}

	void _HandleMessage(IOMessage message) {
		HashtableEx reqArgs = message.args;

		int playerIndex = 0;

		switch (message.code) {
		case IOCmds.SMSG_SET_PLAYER_REQ_DROOM:
			playerIndex = reqArgs.GetInt ("playerIndex");
			SetTitle (playerIndex);
			SetMessage (playerIndex, true);
			break;
		case IOCmds.SMSG_SET_PLAYER_RESP_DROOM:
			bool ok = reqArgs.GetBoolean ("ok");
			playerIndex = reqArgs.GetInt ("playerIndex");
			SetMessage (playerIndex, ok);
			if (!ok) {
				_Close ();
			}
			break;
		}
	}

	public void _Close() {
		DebugInfo.Log("关闭 解散房间确认框");

		AudioController.inst.PlayUIClickEffect();
		uiComp.RemoveFromParent();
		uiComp.Dispose();
		base.Close();

		_inst = null;
	}

	public static void HandleMessage(IOMessage message) {
		if (_inst != null) {
			_inst._HandleMessage (message);
		}
	}

	public static void CloseWindow() {
		if (_inst != null) {
			_inst._Close ();
		}
	}

	/// <summary>
	/// 弹出窗口
	/// </summary>
	public static void Popup()
	{
		DebugInfo.Log("打开 解散房间确认框");
		UIRootController.inst.PopupWindow(new UIGameLeaveConfirm());
	}
}

